package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlEvent entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlEvent
 * @author MyEclipse Persistence Tools
 */

public class MdlEventDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlEventDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String FORMAT = "format";
	public static final String COURSEID = "courseid";
	public static final String GROUPID = "groupid";
	public static final String USERID = "userid";
	public static final String REPEATID = "repeatid";
	public static final String MODULENAME = "modulename";
	public static final String INSTANCE = "instance";
	public static final String EVENTTYPE = "eventtype";
	public static final String TIMESTART = "timestart";
	public static final String TIMEDURATION = "timeduration";
	public static final String VISIBLE = "visible";
	public static final String UUID = "uuid";
	public static final String SEQUENCE = "sequence";
	public static final String TIMEMODIFIED = "timemodified";

	public void save(MdlEvent transientInstance) {
		Transaction trx =getSession().beginTransaction();
		log.debug("saving MdlEvent instance");
		try {
			getSession().save(transientInstance);
			trx.commit();
			trx=null;
			log.debug("save successful");
		} catch (RuntimeException re) {
			
			log.error("save failed", re);
			trx=null;
			throw re;
		}
	}

	public void delete(MdlEvent persistentInstance) {
		log.debug("deleting MdlEvent instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlEvent findById(java.lang.Long id) {
		log.debug("getting MdlEvent instance with id: " + id);
		try {
			MdlEvent instance = (MdlEvent) getSession().get(
					"idv.loveloser.dao.MdlEvent", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlEvent instance) {
		log.debug("finding MdlEvent instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlEvent")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlEvent instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MdlEvent as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByFormat(Object format) {
		return findByProperty(FORMAT, format);
	}

	public List findByCourseid(Object courseid) {
		return findByProperty(COURSEID, courseid);
	}

	public List findByGroupid(Object groupid) {
		return findByProperty(GROUPID, groupid);
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByRepeatid(Object repeatid) {
		return findByProperty(REPEATID, repeatid);
	}

	public List findByModulename(Object modulename) {
		return findByProperty(MODULENAME, modulename);
	}

	public List findByInstance(Object instance) {
		return findByProperty(INSTANCE, instance);
	}

	public List findByEventtype(Object eventtype) {
		return findByProperty(EVENTTYPE, eventtype);
	}

	public List findByTimestart(Object timestart) {
		return findByProperty(TIMESTART, timestart);
	}

	public List findByTimeduration(Object timeduration) {
		return findByProperty(TIMEDURATION, timeduration);
	}

	public List findByVisible(Object visible) {
		return findByProperty(VISIBLE, visible);
	}

	public List findByUuid(Object uuid) {
		return findByProperty(UUID, uuid);
	}

	public List findBySequence(Object sequence) {
		return findByProperty(SEQUENCE, sequence);
	}

	public List findByTimemodified(Object timemodified) {
		return findByProperty(TIMEMODIFIED, timemodified);
	}

	public List findAll() {
		log.debug("finding all MdlEvent instances");
		try {
			String queryString = "from MdlEvent";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlEvent merge(MdlEvent detachedInstance) {
		Transaction trx=getSession().beginTransaction();
		log.debug("merging MdlEvent instance");
		try {
			MdlEvent result = (MdlEvent) getSession().merge(detachedInstance);
			trx.commit();
			log.debug("merge successful");
			trx=null;
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			trx=null;
			throw re;
		}
	}

	public void attachDirty(MdlEvent instance) {
		log.debug("attaching dirty MdlEvent instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MdlEvent instance) {
		log.debug("attaching clean MdlEvent instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}